﻿using System;
using System.Collections.Generic;
using System.Text;
using jade.core;
using MarketSimulator.Utils.Market;
using MarketSimulator.Utils.Maps;
using MarketSimulator.Utils.Attributes;
using System.Windows.Forms;
using MarketSimulator.Agents.Behaviors;

namespace MarketSimulator.Agents.Agents
{
    /// <summary>
    /// Buyer Agent
    /// </summary>
    public class BuyerAgent : Agent
    {
        public static int BuyerAgentCount = 0;

        public static object BLock = new object();

        private Action<string> buyerChat;

        private Action<string> actorsChat;

        private Form TheForm;

        public override void setup()
        {
            try
            {
                object[] args = getArguments();
                TheForm = args[0] as Form;
                buyerChat = args[1] as Action<string>;
                actorsChat = args[2] as Action<string>;

                int selletNo;
                IList<Product> products = null;
                City city = null;
                lock (BLock)
                {
                    selletNo = ++BuyerAgentCount;
                    products = ProductCreator.CreateProductList();
                    city = WorldMap.Cities[(selletNo - 1) % WorldMap.Cities.Count];
                }
                BuyerAttributes buyerAttributes = new BuyerAttributes(this, city, products, WriteToAgentsChat);
                buyerAttributes.RegisterAgent();

                TheForm.Invoke(buyerChat, string.Format("{0} : Location = {1} | Shopping List = {2}", this.getLocalName(), city.Name, buyerAttributes.GetShoppingListString()));

                //Add Behavior Section
                addBehaviour(new BuyerBehavior(buyerAttributes));
            }
            catch (Exception ex)
            {
                TheForm.Invoke(buyerChat, ex.Message);
            }
        } 

        public void WriteToAgentsChat(string message)
        {
            TheForm.Invoke(actorsChat, message);
        }
    }
}
